package com.cool.controller;

import com.cool.dao.ParkinglotMapper;
import com.cool.entity.Parkinglot;
import com.cool.service.ParkinglotService;
import com.cool.util.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping(value = "/api/parkinglots")
public class ParkinglotController {
    @Autowired
    private ParkinglotService parkinglotService;

    @Autowired
    private ParkinglotMapper parkinglotMapper;

    @RequestMapping(method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public ResponseEntity<ArrayList<Parkinglot>> getParkinglots(@RequestParam(value = "lng",defaultValue = "0") String lng,@RequestParam(value = "lat",defaultValue = "0") String lat){
        ArrayList<Parkinglot> parkinglots=new ArrayList<Parkinglot>();
        parkinglots=parkinglotService.selectAll();
        if(lng.equals("0")&&lat.equals("0"))
        {
            if(parkinglots.size()==0) return new ResponseEntity<ArrayList<Parkinglot>>(HttpStatus.NO_CONTENT);
            return new ResponseEntity<ArrayList<Parkinglot>>(parkinglots,HttpStatus.OK);
        }
        else{
            Iterator<Parkinglot> it = parkinglots.iterator();
            while (it.hasNext())
            {
                Parkinglot s= it.next();
                if (MapUtils.algorithm(Double.valueOf(lng),Double.valueOf(lat),Double.valueOf(s.getLng()),Double.valueOf(s.getLat()))>1000)
                {
                    it.remove();
                }
            }
            if(parkinglots.size()==0) return new ResponseEntity<ArrayList<Parkinglot>>(HttpStatus.NO_CONTENT);
            return new ResponseEntity<ArrayList<Parkinglot>>(parkinglots,HttpStatus.OK);
        }
    }

    @RequestMapping(method = RequestMethod.POST,consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<Parkinglot> add(@RequestBody Parkinglot parkinglot){
                if(parkinglotMapper.selectByName(parkinglot.getName())!=null){
                    return new ResponseEntity<Parkinglot>(HttpStatus.NO_CONTENT);
                }
                parkinglotMapper.insert(parkinglot);
                return new ResponseEntity<Parkinglot>(parkinglot,HttpStatus.OK);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public ResponseEntity<Parkinglot> select(@PathVariable int id){
        if(parkinglotMapper.selectByPrimaryKey(id)==null){
            return new ResponseEntity<Parkinglot>(HttpStatus.NO_CONTENT);
        }
        Parkinglot parkinglot=parkinglotMapper.selectByPrimaryKey(id);
        return new ResponseEntity<Parkinglot>(parkinglot,HttpStatus.OK);
    }

    @RequestMapping(value="/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=utf-8")
    public ResponseEntity<Parkinglot> del(@PathVariable int id){
        if(parkinglotMapper.selectByPrimaryKey(id)==null){
            return new ResponseEntity<Parkinglot>(HttpStatus.NO_CONTENT);
        }
        parkinglotMapper.deleteByPrimaryKey(id);
        return new ResponseEntity<Parkinglot>(HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.PUT,consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<Parkinglot> update(@RequestBody Parkinglot parkinglot){
        if(parkinglotMapper.selectByPrimaryKey(parkinglot.getId())==null){
            return new ResponseEntity<Parkinglot>(HttpStatus.NO_CONTENT);
        }
        parkinglotMapper.updateByPrimaryKeySelective(parkinglot);
        Parkinglot result=parkinglotMapper.selectByPrimaryKey(parkinglot.getId());
        return new ResponseEntity<Parkinglot>(result,HttpStatus.OK);
    }

}
